package agent;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import labyrinth.Map;
import agent.moves.DirectionMove;
import agent.moves.IAgentMove;
import agent.moves.InvalidDirectionException;

public class HumanAgent extends IAgent implements KeyListener {

	DirectionMove nextMove;
	Map mPosition;
	
	public HumanAgent() {
		try {
			nextMove = new DirectionMove(DirectionMove.STOP);
		} catch (InvalidDirectionException e) {
			e.printStackTrace();
		} 		
	}
	
	@Override
	public IAgentMove getNextMove(Map position, IAgentMove move) {
		mPosition = position;
		IAgentMove result = nextMove;
		try {
			nextMove = new DirectionMove(DirectionMove.STOP);
		} catch (InvalidDirectionException e) {
			e.printStackTrace();
		} 
		return result;
	}

	@Override
	public void keyPressed(KeyEvent event) {
		int keycode = event.getKeyCode();
		switch (keycode) {
		case KeyEvent.VK_SPACE:
			System.out.println(mPosition + "\n---\n");
			break;
		case KeyEvent.VK_DOWN:
			System.out.println("Down");
			try {
				nextMove = new DirectionMove(DirectionMove.DOWN);
			} catch (InvalidDirectionException e) {
				e.printStackTrace();
			} 
			break;
		case KeyEvent.VK_UP:
			try {
				nextMove = new DirectionMove(DirectionMove.UP);
			} catch (InvalidDirectionException e) {
				e.printStackTrace();
			} 
			break;
		case KeyEvent.VK_RIGHT:
			try {
				nextMove = new DirectionMove(DirectionMove.RIGHT);
			} catch (InvalidDirectionException e) {
				e.printStackTrace();
			} 
			break;
		case KeyEvent.VK_LEFT:
			try {
				nextMove = new DirectionMove(DirectionMove.LEFT);
			} catch (InvalidDirectionException e) {
				e.printStackTrace();
			} 
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}
